//
//  ReadyFinishViewController.m
//  51易借
//
//  Created by 张睿 on 2017/9/12.
//  Copyright © 2017年 张睿. All rights reserved.
//

#import "ReadyFinishViewController.h"
#import "ReadyTableViewCell.h"
#import "LoanModel.h"
#import "ImmediatelyRepayController.h"

@interface ReadyFinishViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)NSMutableArray * dataArr;
@property (nonatomic, weak)UIView * topView;
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic,strong)ParaModel * para;

@property (nonatomic,strong)UIImageView * faceView;
@property (nonatomic,strong)UILabel * labelView;

@end

@implementation ReadyFinishViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupNavBar];
    [self setupUI];
    [self getMessages];
}


- (void)setupNavBar{
    
    self.title = @"已结清";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(15 * SCREENH6, 5 * SCREENH6, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToLastView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}



- (void)setupUI{
    
    //    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.view.backgroundColor = BACK_COLOR;
    
    UIView * topView = [UIView new];
    
    topView.frame = CGRectMake(0, 0, ScreenWidth, 64);
    topView.backgroundColor = TOTAL_COLOR;
//    topView.backgroundColor = ColorWithRGB(0xff7907);
    
    self.topView = topView;
    
    [self.view addSubview:self.topView];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREENH_HEIGHT - 64)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.bounces = YES;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[ReadyTableViewCell class] forCellReuseIdentifier:@"ReadyCell"];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.para.page = @(1);
        [self getMessages];
    }];
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        self.para.page = @([self.para.page integerValue]+1);
        [self getMessages];
        
    }];
    [self.view addSubview:self.tableView];
    
    
    self.faceView = [UIImageView new];
    self.faceView.image = [UIImage imageNamed:@"无"];
    //    self.faceView.hidden = YES;
    
    self.labelView = [UILabel new];
    self.labelView.font = [UIFont systemFontOfSize:15*SCREENH6];
    self.labelView.textColor = [UIColor grayColor];
    self.labelView.text = @"目前无待还款账单";
    //    self.labelView.hidden = YES;
    
    [self.view addSubview:self.faceView];
    [self.view addSubview:self.labelView];
    
    [self.faceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.centerY.mas_equalTo(self.view).offset(-10 * SCREENH6);
        make.height.mas_equalTo(82 * SCREENH6);
        make.width.mas_equalTo(94 * SCREENH6);
        
    }];
    [self.labelView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.faceView.mas_bottom).offset(20 * SCREENH6);
        make.height.mas_equalTo(15 * SCREENH6);
        
    }];
}


- (void)viewDidLayoutSubviews{
    
//    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.leading.trailing.mas_equalTo(self.view);
//        make.height.mas_equalTo(64 * SCREENH6);
//    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    //    NSLog(@"==%lu",(unsigned long)self.modelArray.count);
    
    return self.dataArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ReadyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ReadyCell" forIndexPath:indexPath];
    
    cell.model = self.dataArr[indexPath.row];
    //model
    //    SystemMessageListModel *model = self.dataArray[indexPath.item];
    //    cell.tag = indexPath.item;
    //    cell.model = model;
//    cell.model = self.modelArray[indexPath.row];
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130 * SCREENH6;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HIDDEN_TAB;
    
    ReadyTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    ImmediatelyRepayController * immedVC = [ImmediatelyRepayController new];
    
    immedVC.loanID = cell.model.applyNo;
    
    [self.navigationController pushViewController:immedVC animated:YES];
}


- (void)backToLastView{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)getMessages
{
    
//    loadMyMsgList
    [[NetworkTool shareManager] POST:[Interfacefile loadFinishedRecordList] Param:[self.para toDictionary] andWaitStr:@"" waitView:self.view success:^(id  _Nullable responseObject) {
        if([[responseObject objectForKey:@"code"]intValue]==0)
        {
            if([self.para.page integerValue]==1)
            {
                [self.dataArr removeAllObjects];
            }
            [self.dataArr addObjectsFromArray:[LoanModel arrayOfModelsFromDictionaries:[responseObject objectForKey:@"data"] error:nil]];
            
            if (self.dataArr.count > 0) {
                
                self.faceView.hidden = YES;
                self.labelView.hidden = YES;
                self.tableView.hidden = NO;
               
            }else{
                
                self.faceView.hidden = NO;
                self.labelView.hidden = NO;
                self.tableView.hidden = YES;
            }
            
        }else{
            
            [self showHint:[responseObject objectForKey:@"message"]];
            
            
        }
        
        
        
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
    } andFailure:^(NSError * _Nonnull error) {
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        [self showHint:@"网络错误"];
        
    }];
    
}


-(ParaModel*)para
{
    if(!_para)
    {
        _para = [ParaModel new];
        _para.limit = @(10);
        _para.page = @(1);
    }
    return _para;
}



-(NSMutableArray*)dataArr
{
    if(!_dataArr)
    {
        _dataArr = [NSMutableArray array];
        
    }
    return _dataArr;
}



@end
